//
//  CICropViewController.m
//  PhotoCrop
//
//  Created by 刘 真 on 4/20/16.
//  Copyright © 2016 刘 真. All rights reserved.
//

#import "CropViewController.h"

@interface CropViewController () <UIScrollViewDelegate>
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *border;
@end

@implementation CropViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    
    NSAssert(self.whRatio > 0, @"whRatio用于指定裁剪框的宽高比例，不能为0");
    
    //隐藏导航条
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    //添加ScrollView
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.scrollView.maximumZoomScale = 10;
    self.scrollView.minimumZoomScale = 1;
    self.scrollView.delegate = self;
    self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];

    //计算遮罩框的大小
    CGSize maskSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.width/self.whRatio);
    CGSize imageSize = self.editImage.size;
    
    CGFloat wScale = maskSize.width/imageSize.width;
    CGFloat hScale = maskSize.height/imageSize.height;
    CGFloat scale = MAX(wScale, hScale);
    imageSize = CGSizeMake(imageSize.width*scale, imageSize.height*scale);
    
    //添加图片展示图层
    self.imageView = [[UIImageView alloc] initWithImage:nil];
    self.imageView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
    self.imageView.frame = CGRectMake(0, 0, imageSize.width, imageSize.height);
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.scrollView.contentSize = self.imageView.frame.size;
    [self.scrollView addSubview:self.imageView];
    
    //计算遮罩区域的高度
    CGFloat h = self.view.frame.size.width/self.whRatio;
    h = (self.view.frame.size.height - h) / 2.0f;
    self.scrollView.contentInset = UIEdgeInsetsMake(h - 20, 0, h, 0);

    //添加遮罩
    UIView *top = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, h)];
    top.userInteractionEnabled = NO;
    top.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.3048760776];
    [self.view addSubview:top];
    
    UIView *bottom = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - h, self.view.frame.size.width, h)];
    bottom.userInteractionEnabled = NO;
    bottom.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.3048760776];
    [self.view addSubview:bottom];
    
    //添加选区框
    CGFloat y = CGRectGetMaxY(top.frame);
    UIView *border = [[UIView alloc] initWithFrame:CGRectMake(0, y, CGRectGetWidth(top.frame), CGRectGetMinY(bottom.frame) - y)];
    border.backgroundColor = [UIColor clearColor];
    border.layer.borderWidth = 1.5;
    border.layer.borderColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1.0].CGColor;
    border.userInteractionEnabled = NO;
    [self.view addSubview:border];
    self.border = border;
    
    //添加底部动作条
    CGFloat barHeight = 72;
    UIView *actionBar = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame) - barHeight, CGRectGetWidth(self.view.frame), barHeight)];
    actionBar.contentMode = UIViewContentModeCenter;
    actionBar.backgroundColor = [UIColor colorWithRed:0.0784 green:0.0784 blue:0.0784 alpha:1.0];
    actionBar.tintColor = [UIColor whiteColor];

    UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancel setTitle:@"取消" forState:UIControlStateNormal];
    [cancel setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [cancel addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
    [actionBar addSubview:cancel];
    [cancel sizeToFit];
    cancel.frame = CGRectMake(20, 0, CGRectGetWidth(cancel.frame), 72);
    
    UIButton *choose = [UIButton buttonWithType:UIButtonTypeCustom];
    [choose setTitle:@"选择" forState:UIControlStateNormal];
    [choose setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [choose addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchUpInside];
    [actionBar addSubview:choose];
    [choose sizeToFit];
    choose.frame = CGRectMake(CGRectGetWidth(actionBar.frame) - CGRectGetWidth(choose.frame) - 20, 0, CGRectGetWidth(choose.frame), 72);
    [self.view addSubview:actionBar];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    //在viewDidLoad中直接将图片赋值给imageView的话，直接拍的照片显示会有问题，不知道为何
    self.imageView.image = self.editImage;
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
}

- (void)cancel:(id)sender {
    if ([self.navigationController isKindOfClass:[UIImagePickerController class]]) {
        UIImagePickerController *picker = (UIImagePickerController *)self.navigationController;
        if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
            [self dismissViewControllerAnimated:YES completion:nil];
            return;
        }
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)done:(id)sender {
    CGFloat h = self.view.frame.size.width/self.whRatio;
    CGSize imageSize = CGSizeMake(self.view.frame.size.width, h);
    UIGraphicsBeginImageContextWithOptions(imageSize, YES, 0.0);
    CGRect frame = [self.scrollView convertRect:self.imageView.frame toView:self.view];
    frame = [self.view convertRect:frame toView:self.border];
    [self.editImage drawInRect:frame];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    if (self.cropImage) {
        self.cropImage(image);
    }
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.imageView;
}

@end
